/**
 * 
 */
package com.py.credit.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.py.credit.common.ServiceException;
import com.py.credit.dao.ConcernedSubjectDao;
import com.py.credit.entity.ConcernedSubject;
import com.py.credit.service.CreditConcernedService;
import com.py.credit.service.excel.read.service.ExcelReadService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

/**
 * @author dd
 *
 */
@Service
public class CreditConcernedServiceImpl implements CreditConcernedService {
	@Autowired
	private ConcernedSubjectDao concernedSubjectDao;

	@Autowired
	private ExcelReadService excelReadService;

	@Override
	public List<Map<String, Object>> getByConditionForPage(Long userId, List<String> showColumns,
			String[] additionalColumns, String condition, Page page) {

		if (CollectionUtils.isEmpty(showColumns)) {

			throw new ServiceException("至少选择一列导出");

		}
		List<String> conditions = SQLBuilder.createConditionSql(condition);

		return concernedSubjectDao.selectByConditionForPage(userId, showColumns, additionalColumns, conditions, page);

	}

	@Override
	public void add(ConcernedSubject record) {

		concernedSubjectDao.insertSelective(record);

	}

	@Override
	public Map<String, Integer> add(CommonsMultipartFile file, Long userId) throws IOException {

		Sheet readExcelFileForSheet = excelReadService.readExcelFileForSheet(file.getInputStream());
		Map<String, Integer> addRecordsFromSheet = addRecordsFromSheet(readExcelFileForSheet, userId);
		return addRecordsFromSheet;

	}

	@Override
	public void deletById(int id) {

		concernedSubjectDao.deleteByPrimaryKey(id);

	}

	private Map<String, Integer> addRecordsFromSheet(Sheet st, Long userId) {

		Map<String, Integer> result = new HashMap<String, Integer>();
		boolean reStart = true;
		int i = 0;
		List<Map<String, Object>> listDataMap = null;
		int totalCount = 0;
		int successCount = 0;
		int start = 0;
		int end = 0;
		while (reStart) {
			start = i * 1000;
			end = (i + 1) * 1000;
			if (end > st.getLastRowNum()) {
				end = st.getLastRowNum();
			}

			listDataMap = excelReadService.readExcelData(start, end, st);
			int size = listDataMap.size();
			totalCount += size;
			if (listDataMap.isEmpty()) {
				reStart = false;
			} else {

				for (Map<String, Object> map : listDataMap) {

					if (map.get("name") != null && existInUnqiue(map.get("name").toString())
							&& !exsit(map.get("name").toString(), userId)) {

						{

							ConcernedSubject record = new ConcernedSubject();
							record.setSubjectName(map.get("name").toString());
							record.setLevel(map.get("level").toString());
							record.setUserId(userId);
							concernedSubjectDao.insertSelective(record);
							successCount++;

						}
					}

				}

			}

			i++;
		}

		result.put("successCount", successCount);

		result.put("failCount", totalCount - successCount);

		return result;

	}

	private boolean existInUnqiue(String name) {

		return concernedSubjectDao.selectUnqiueByName(name) > 0;
	}

	@Override
	public List<String> getUniqueByName(String name) {
		return concernedSubjectDao.selectUniqueByName(name);
	}

	@Override
	public List<Map<String, Integer>> getSubjectChangedSubscriber() {

		return concernedSubjectDao.selectSubjectChangedSubscriber();

	}

	@Override
	public void addNewRecord(int id, int recordId, String resourceId) {

		concernedSubjectDao.addNewRecord(id, recordId, resourceId);
	}

	@Override
	public void updateCheckStatus(String checkStatus, int id, String modifyTime) {
		concernedSubjectDao.updateCheckStatus(checkStatus, id, modifyTime);

	}

	@Override
	public void updatePushStatus(String pushStatus, List<Map<String, Integer>> changedSubject) {

		concernedSubjectDao.updatePushStatus(pushStatus, changedSubject);
	}

	@Override
	public boolean exsit(String subjectName, Long userid) {

		return concernedSubjectDao.selectCount(subjectName, userid) > 0;
	}

}
